package com.thunder.rebot.rebotclient.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.iml.SteerWheelListener;

public class CircleView extends View {

    private int circleWidth = 60; // 圆环直径
    private int circleColor = Color.argb(50, 50, 50, 50);
//    private int circleColor = Color.alpha(Color.BLACK);
    private int innerCircleColor = Color.rgb(250, 0, 0);//150
    private int backgroundColor = Color.argb(0,0, 0, 0);
    private int paintColor = Color.rgb(255,250,250);
//    private int backgroundColor = Color.TRANSPARENT;
    private Paint paint = new Paint();
    int center = 0;
    int innerRadius = 0;//0
    private float innerCircleRadius = 0;
    private float smallCircle = 5;//10 内圆的半径或者是直径，记白色的圆
    private int outCircleSpace = 20;//外环之间间隔缝隙
//    public Dir dir = Dir.UP;
    public Dir dir  = Dir.UNDEFINE;

    public CircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleView(Context context) {
        super(context);

        // paint = new Paint();
    }

    public CircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measuredHeight = measureHeight(heightMeasureSpec);
        int measuredWidth = measureWidth(widthMeasureSpec);

        setMeasuredDimension(measuredWidth, measuredHeight);
        center = getMeasuredWidth() / 2;
        innerRadius = (center - circleWidth / 2 - 10);// 圆环 2   10
        innerCircleRadius = center / 3;//
        this.setOnTouchListener(onTouchListener);
    }

    /**
     * 测量宽度
     *
     * @param measureSpec
     * @return
     */
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        int result = 0;

        if (specMode == MeasureSpec.AT_MOST) {
            result = getWidth();
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }

    /**
     * 测量高度
     *
     * @param measureSpec
     * @return
     */
    private int measureHeight(int measureSpec) {

        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        int result = 0;

        if (specMode == MeasureSpec.AT_MOST) {

            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }

    /**
     * 开始绘制
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        initBackGround(canvas);
        drawDirTriangle(canvas, dir);//这个是默认是向上箭头

    }

    /**
     * 绘制方向小箭头
     *
     * @param canvas
     */
    private void drawDirTriangle(Canvas canvas, Dir dir) {
        paint.setColor(innerCircleColor);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.FILL);

        switch (dir) {
            case UP:
                drawUpTriangle(canvas);
                break;
            case DOWN:
                drawDownTriangle(canvas);
                break;
            case LEFT:
                drawLeftTriangle(canvas);
                break;
            case RIGHT:
                drawRightTriangle(canvas);
                break;
            case CENTER:
                invalidate();
                break;
            default:
                break;
        }

        paint.setColor(paintColor);

        /*绘制文本*/
        String testString = "停止";
        paint.setTextSize(40);
        paint.setTextAlign(Paint.Align.CENTER);
        Rect bounds = new Rect();
        paint.getTextBounds(testString, 0, testString.length(), bounds);
        canvas.drawText(testString, getMeasuredWidth()/2 - bounds.width()/2 +40, getMeasuredHeight()/2 + bounds.height()/2,paint);

        canvas.drawCircle(center, center, smallCircle, paint);
        // canvas.drawText(text, center, center+40, paint);

    }

    /**
     * 绘制向右的小箭头
     *
     * @param canvas
     */
    private void drawRightTriangle(Canvas canvas) {
        Path path = new Path();
        path.moveTo(center, center);
        double sqrt2 = innerCircleRadius / Math.sqrt(2);
        double pow05 = innerCircleRadius * Math.sqrt(2);
        path.lineTo((float) (center + sqrt2), (float) (center - sqrt2));
        path.lineTo((float) (center + pow05), center);
        path.lineTo((float) (center + sqrt2), (float) (center + sqrt2));
        canvas.drawPath(path, paint);
        paint.setColor(paintColor);
        canvas.drawLine(center, center, center + innerCircleRadius, center, paint);

        drawOnclikColor(canvas, Dir.RIGHT);
    }

    /**
     * 绘制想左的小箭头
     *
     * @param canvas
     */
    private void drawLeftTriangle(Canvas canvas) {
        Path path = new Path();
        path.moveTo(center, center);
        double sqrt2 = innerCircleRadius / Math.sqrt(2);
        double pow05 = innerCircleRadius * Math.sqrt(2);
        path.lineTo((float) (center - sqrt2), (float) (center - sqrt2));
        path.lineTo((float) (center - pow05), center);
        path.lineTo((float) (center - sqrt2), (float) (center + sqrt2));
        canvas.drawPath(path, paint);

        paint.setColor(paintColor);
        canvas.drawLine(center, center, center - innerCircleRadius, center, paint);

        drawOnclikColor(canvas, Dir.LEFT);

    }

    /**
     * 绘制向下的小箭头
     *
     * @param canvas
     */
    private void drawDownTriangle(Canvas canvas) {
        Path path = new Path();
        path.moveTo(center, center);
        double sqrt2 = innerCircleRadius / Math.sqrt(2);
        double pow05 = innerCircleRadius * Math.sqrt(2);
        path.lineTo((float) (center - sqrt2), (float) (center + sqrt2));
        path.lineTo(center, (float) (center + pow05));
        path.lineTo((float) (center + sqrt2), (float) (center + sqrt2));
        canvas.drawPath(path, paint);

        paint.setColor(paintColor);
        canvas.drawLine(center, center, center, center + innerCircleRadius, paint);

        drawOnclikColor(canvas, Dir.DOWN);
    }

    /**
     * 点击的时候绘制黑色的扇形
     *
     * @param canvas
     * @param dir
     */
    private void drawOnclikColor(Canvas canvas, Dir dir) {
        paint.setColor(getContext().getResources().getColor(R.color.hotpink));
        paint.setAlpha(90);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(circleWidth);//100
        switch (dir) {
            case UP:
                canvas.drawArc(new RectF(center - innerRadius , center - innerRadius, center + innerRadius, center
                        + innerRadius), 228, 84, false, paint);
                break;
            case DOWN:
                canvas.drawArc(new RectF(center - innerRadius, center - innerRadius, center + innerRadius, center
                        + innerRadius), 48, 84, false, paint);
                break;
            case LEFT:
                canvas.drawArc(new RectF(center - innerRadius, center - innerRadius, center + innerRadius, center
                        + innerRadius), 138, 84, false, paint);
                break;
            case RIGHT:
                canvas.drawArc(new RectF(center - innerRadius, center - innerRadius, center + innerRadius, center
                        + innerRadius), -42, 84, false, paint);
                break;

            default:
                break;
        }

        paint.setStyle(Paint.Style.FILL);
    }

    /**
     * 绘制像向上的箭头
     *
     * @param canvas
     */
    private void drawUpTriangle(Canvas canvas) {
        Path path = new Path();
        path.moveTo(center, center);
        double sqrt2 = innerCircleRadius / Math.sqrt(2);
        double pow05 = innerCircleRadius * Math.sqrt(2);

        path.lineTo((float) (center - sqrt2), (float) (center - sqrt2));
        path.lineTo(center, (float) (center - pow05));
        path.lineTo((float) (center + sqrt2), (float) (center - sqrt2));
        canvas.drawPath(path, paint);

        paint.setColor(paintColor);
        canvas.drawLine(center, center, center, center - innerCircleRadius, paint);

        drawOnclikColor(canvas, Dir.UP);
    }

    /**
     * 绘制基本的背景， 这包括了三个步骤：1.清空画布 2.绘制外圈的圆 3.绘制内圈的圆
     *
     * @param canvas
     */
    private void initBackGround(Canvas canvas) {
        clearCanvas(canvas);
        drawBackCircle(canvas);
        drawInnerCircle(canvas);

    }

    /**
     * 绘制中心白色小圆
     *
     * @param canvas
     */
    private void drawInnerCircle(Canvas canvas) {
        paint.setColor(innerCircleColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        canvas.drawCircle(center, center, innerCircleRadius, paint);
    }

    /**
     * 绘制背景的圆圈和隔线
     *
     * @param canvas
     */
    private void drawBackCircle(Canvas canvas) {
        paint.setColor(circleColor);
        paint.setStrokeWidth(circleWidth);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        canvas.drawCircle(center, center, innerRadius, paint); // 绘制圆圈

        //画三角形
        //top
        Paint paint1 = new Paint();
        paint1.setColor(Color.GRAY);
        Path path=new Path();

        path.moveTo(center  -15 ,center - innerRadius + 5);
        path.lineTo(center  + 15,center - innerRadius + 5);
        path.lineTo((center ) ,center - innerRadius - 10);

        path.close();
        canvas.drawPath(path, paint1);


        //right
        Paint paint2 = new Paint();
        paint2.setColor(Color.GRAY);
        Path path2=new Path();

        path2.moveTo(center +innerRadius   ,center -  15);
        path2.lineTo(center  +innerRadius  ,center +  15);
        path2.lineTo((center  +innerRadius + 15 ) ,center );

        path2.close();
        canvas.drawPath(path2, paint2);

        //bottom
        Paint paint3 = new Paint();
        paint3.setColor(Color.GRAY);
        Path path3=new Path();

        path3.moveTo(center  +15 ,center + innerRadius - 5);
        path3.lineTo(center  -15,center + innerRadius - 5);
        path3.lineTo((center ) ,center + innerRadius + 10);

        path3.close();
        canvas.drawPath(path3, paint3);

        //left
        Paint paint4 = new Paint();
        paint4.setColor(Color.GRAY);
        Path path4=new Path();

        path4.moveTo(center  -innerRadius ,center - 15);
        path4.lineTo(center  - innerRadius,center  + 15);
        path4.lineTo((center - innerRadius -15) ,center );

        path4.close();
        canvas.drawPath(path4, paint4);


        paint.setColor(Color.GRAY);//innerCircleColor   backgroundColor
        paint.setAlpha(40);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(outCircleSpace);//外环的间隔
        canvas.drawLine(center -55, center -55, center - innerRadius +10, center - innerRadius +10, paint);
        canvas.drawLine(center + 55, center -55, center  + innerRadius -10, center - innerRadius +10, paint);
        canvas.drawLine(center -55, center+55, center - innerRadius + 10, center + innerRadius - 15, paint);
        canvas.drawLine(center + 55, center + 55 , center + innerRadius -10, center +innerRadius -10, paint);

    }

    /**
     * 清空画布
     *
     * @param canvas
     */
    private void clearCanvas(Canvas canvas) {
        canvas.drawColor(backgroundColor);
    }
    private SteerWheelListener onSteerWheelListener;
    public void setOnSteerWheelListener(SteerWheelListener onSteerWheelListener){
        this.onSteerWheelListener = onSteerWheelListener;
    }

    OnTouchListener onTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            Dir tmp = Dir.UNDEFINE;
            if (event.getAction() == MotionEvent.ACTION_UP){
                if ((tmp = checkDir(event.getX(), event.getY())) != Dir.UNDEFINE) {
                    dir = tmp;
                    onSteerWheelListener.direction(dir);
                    invalidate();
                }
            }

            return true;
        }

        /**
         * 检测方向
         *
         * @param x
         * @param y
         * @return
         */
        private Dir checkDir(float x, float y) {
            Dir dir = Dir.UNDEFINE;

            if (Math.sqrt(Math.pow(y - center, 2) + Math.pow(x - center, 2)) < innerCircleRadius) {// 判断在中心圆圈内
                dir = Dir.CENTER;
                System.out.println("----中央");
            } else if (y < x && y + x < 2 * center) {
                dir = Dir.UP;
                System.out.println("----向上");
            } else if (y < x && y + x > 2 * center) {
                dir = Dir.RIGHT;
                System.out.println("----向右");
            } else if (y > x && y + x < 2 * center) {
                dir = Dir.LEFT;
                System.out.println("----向左");
            } else if (y > x && y + x > 2 * center) {
                dir = Dir.DOWN;
                System.out.println("----向下");
            }

            return dir;
        }

    };

    /**
     * 关于方向的枚举
     *
     * @author Administrator
     *
     */
    public enum Dir {
        UP, DOWN, LEFT, RIGHT, CENTER, UNDEFINE
    }

}
